//
//  XXJEssenceViewController.m
//  不得姐
//
//  Created by 许小军 on 16/9/1.
//  Copyright © 2016年 wyzc. All rights reserved.
//

#import "XXJEssenceViewController.h"
#import "UIBarButtonItem+XXJBarButtonItem.h"
#import "AllTableViewController.h"
#import "VideoTableViewController.h"
#import "VioceTableViewController.h"
#import "PictureTableViewController.h"
#import "WordTableViewController.h"

@interface XXJEssenceViewController ()<UIScrollViewDelegate>
@property(strong,nonatomic)UIView * titleView;
@property(weak,nonatomic)UIButton * previousClickedBtn;
@property(weak,nonatomic)UIView * line;
@property(weak,nonatomic)UIScrollView * scrollerView;
@end

@implementation XXJEssenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //self.view.backgroundColor=[UIColor orangeColor];
    [self addchildView];
    
    [self setNavBar];
    
    [self setupScrollVew];
    
    [self setupTitleView];
    
    [self setUpLine];
    
    UIButton * firstBtn=self.titleView.subviews[0];
    [self btnClick:firstBtn];
    
}

-(void)addchildView
{
    [self addChildViewController:[[AllTableViewController alloc]init]];
    [self addChildViewController:[[VideoTableViewController alloc]init]];
    [self addChildViewController:[[VioceTableViewController alloc]init]];
    [self addChildViewController:[[PictureTableViewController alloc]init]];
    [self addChildViewController:[[WordTableViewController alloc]init]];

    
}


-(void)setupScrollVew
{
    UIScrollView * scrollerView=[[UIScrollView alloc]init];
    _scrollerView=scrollerView;
    scrollerView.delegate=self;
    scrollerView.pagingEnabled=YES;
    scrollerView.showsHorizontalScrollIndicator=NO;
    scrollerView.showsVerticalScrollIndicator=NO;
    scrollerView.scrollsToTop=NO;
    scrollerView.frame=self.view.bounds;
    scrollerView.backgroundColor=[UIColor grayColor];
    scrollerView.contentSize=CGSizeMake(self.view.cur_w*5, 0);
    [self.view addSubview:scrollerView];
    
}

-(void)setupTitleView
{
    UIView * titleView=[[UIView alloc]init];
    self.titleView=titleView;
    titleView.frame=CGRectMake(0, 64, self.view.cur_w, 35);
    titleView.backgroundColor=[[UIColor whiteColor]colorWithAlphaComponent:0.5];
    [self.view addSubview:titleView];
    
    
    [self setupTitleBtn];
    
    
}

-(void)setupTitleBtn
{
    
    CGFloat btnW=self.view.cur_w/5.0;
    CGFloat btnH=self.titleView.cur_h;
    NSArray * arr=@[@"全部",@"视频",@"声音",@"图片",@"段子"];
    for (int i=0; i<5; i++)
    {
        UIButton * btn=[[UIButton alloc]init];
        btn.frame=CGRectMake(btnW*i, 0, btnW, btnH);
        btn.tag=100+i;
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [self.titleView addSubview:btn];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

-(void)btnClick:(UIButton *)btn
{
    if (_previousClickedBtn==btn) {
        [[NSNotificationCenter defaultCenter]postNotificationName:titleBtnDoubleClick object:nil];
    }
    _previousClickedBtn.selected=NO;
    btn.selected=YES;
    _previousClickedBtn=btn;
    NSInteger index=[self.titleView.subviews indexOfObject:btn];
    [UIView animateWithDuration:0.25 animations:^{
        _line.cur_w=[btn.currentTitle sizeWithAttributes:@{NSFontAttributeName:btn.titleLabel.font}].width+10;
        _line.cur_centerX=btn.cur_centerX;
         _scrollerView.contentOffset=CGPointMake(index *_scrollerView.cur_w , 0);
    }completion:^(BOOL finished) {
        UIView * view=self.childViewControllers[index].view;
        if (view.superview) {
            return ;
        }
       
        view.frame=CGRectMake(index * _scrollerView.cur_w, 0, _scrollerView.cur_w, _scrollerView.cur_h);
        
        [_scrollerView addSubview:view];
    }];
    
    for (int i=0; i<self.childViewControllers.count; i++) {
        UIViewController * childVc=self.childViewControllers[i];
        if (!childVc.isViewLoaded) {
            continue;
        }
        UIScrollView * scroller=(UIScrollView *)childVc.view;
        if (![scroller isKindOfClass:[UIScrollView class]]) {
            continue;
        }
        if (i==index) {
            scroller.scrollsToTop=YES;
        }
        else
        {
            scroller.scrollsToTop=NO;
        }
    }
}

-(void)setUpLine
{
    UIView * line=[[UIView alloc]init];
    _line=line;
    line.cur_h=2;
    line.cur_y=self.titleView.cur_h-line.cur_h;
    line.backgroundColor=[UIColor redColor];
    [self.titleView addSubview:line];
    
}

-(void)setNavBar
{
    self.navigationItem.titleView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MainTitle"]];
    
    
    
    
    self.navigationItem.leftBarButtonItem=[UIBarButtonItem setNormalImage:[UIImage imageNamed:@"nav_item_game_icon"] SelectedImage:[UIImage imageNamed:@"nav_item_game_click_icon"] addTarget:self action:@selector(leftBtn)];
    
    self.navigationItem.rightBarButtonItem=[UIBarButtonItem setNormalImage:[UIImage imageNamed:@"navigationButtonRandom"] SelectedImage:[UIImage imageNamed:@"navigationButtonRandomClickN"] addTarget:self action:@selector(rightBtn)];
    
    
}

#pragma mark --UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index=_scrollerView.contentOffset.x/_scrollerView.cur_w;
    UIButton * btn=self.titleView.subviews[index];
    [self btnClick:btn];
}

-(void)leftBtn
{
    NSLog(@"1111");
}
-(void)rightBtn
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
